package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000137694806";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCDp8ayiLutarsLGaHWufZ6i9Fi4v90UY6kPaRZZIipY68Sez0jfRnyzKWC5Q+7O1hMV0WFMWNFWAyHCubbVGysnZljupDNxfbl8S26Xyo7fpDx22Feestmtfz2MZ0qjKVXreA07cFk11mgGNi2ejtyi2dQd3LRMquDiSvlUae1kNATZrYV6j3jMdsEejW+Xmxud54itgm+0BTgL4reXZPI9V8qpkzTVoKN177hQ75IM3bkXGpOSzQHb9GTa6CCXKBZ6yjdN9rV5lPW+vE3Lx1bNJbf3ghHPpOENi3Devz2a8LTmDtlDDQWI12EcT6ZEL9AfVgsR7Tycdnnl+M4BtDBAgMBAAECggEAE/npFD3a/D0tlEmXXNVqqL1+T9rRl/yoABO6VDYE5m7RqaQ5aqEYxtZ2Zp4K/QXMKAk16zfLMEJj5DmrPaxZsPdYxuEQB6j6BXnKznSSG+TgaVI4O/W11yfV8iUlC8e7bspbMqzAo2RpnPkoKMfzLTYpvvPg4A0oKRUvK+Rv5gSTLcA8lrPEttwP1saNbNU/4nqIy02TXbt0+fJ9rCabeSPIAqnlL4nTBcJejYwWa7ji7WG1N+Sr/2Bm7NB7QakIQ+Hqhv3W93W1KOFjs66j0sG1kOIJugDJ+ON+Wpdy64SWL6MuGzD0M4nRp6OjIpRoCmNxXjG+gL9Nv/pD4FiG4QKBgQDGMBooDg2i8mnc0/fR6YIqoafUiHbZhz3w/gi98QZ2RuIO4tkBnWWYlM0h1uTLuk2aNqFirpLecJ6pEqxx1mdjg5Eq90PwS9AoQySciVp/Nbmp3T1WUi1WstCmVHxgUyTxFjUV5g2eT+P2rQdVjjp+Q9o2VAGGilIDcSlr6kc0ewKBgQCqD0j+TXb7dwr2FZIj5hdzPGBIUcsQnv1fQ8mDb831w//so5uptNV0y8yCrj3kjeptgie0yhkIQJ2B9VTol6oD3h8hTIITl/iZl8mmT9sD2fV1slDsjMpJiztT/pnCQDnM5OUpPsLfzU0RIJjL18W1yx5mzArzpVlWYM5XUFAA8wKBgHtTx5MRW68AhpviU56OXm/E9LkuyjaHECZ9DleUOt8zMMzuoPd+SJBwWE4seWfFYTELudW8qKw/DJxNgH2+W8vzPUytXduUcc38KjY2ytfKSOTJRbgHnysKTwiSHmuakaxsk1AzPlo7gLIp7hxxSzjzZEDWebSNMoftzUbYHMMtAoGAWaMU5nYOcR2eOXfFQAbX9XWtinESiABFrSEd5pnYBeL50tGqCxm2KYoCkh8loILtPiuMok6kQyVL4GRgDPlkNAWwguzA43MHJm5La85Lswv/xbnnzceoGHu7GdrpUOppjQZZhBQAw9aC+LQl1cw1+kzGCYkjfKl7HXyJRpyejuECgYBAVnlPCZsxOLR0GNiq7gi54rFMzv8RczKzziHstrPZTD1BrddYaZC3PgpBfcSuUq5ufu/5iEg0dYgGhx/FTMrqhm+ojcR/1L0pDzXf0Q9Gj3VvKogC4my7yQsCOLpn7QyADRrHUAZc52Zh6Mn24Vs1VAYR+5v2rJcRkby+UbG/lQ==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsKxsSoQyIUab/Z5iOBsajMi8QFXWLTFkWF2AJnDCqWBtnJ2HdbNl1f3JPvJYWRWjEWfH9d0VPr6EMA8tdigNPAwOwDq4+wIbf+N9XgLCX0SbhY5UabiO4v//88+9tP21BMK7D9m73Ts2AgvxWA3wnDYJJlKgV9zGutiv5K39YLmJ3Lnr1VYirqqcprP9mFXdL+IAhMKhFk718ykvSgGQIZaD3L4P/2BpTGhIK0RNngM3Ui5FeTV9nYcgmW/4YdrHwxz4W/4R/bEz88fnsfDnB7TVtv4crx7z3ld8D76XnumqAFuLdbhNBZU+rKTSGHEazy6nOPao/3CqHL06PapvswIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
//    private  String notify_url = "http://83738zxts239.vicp.fun/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";
    private  String notify_url = "http://83738zxts239.vicp.fun/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
//    private  String return_url = "http://83738zxts239.vicp.fun/alipay.trade.page.pay-JAVA-UTF-8/return_url.jsp";
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    private String timeout = "1m";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
